/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* Generated By:JJTree: Do not edit this line. AstIdentifier.java */

// package org.apache.el.parser;
package com.gdevelop.gwtxp.rebind.el;


import com.google.gwt.core.ext.typeinfo.JClassType;

import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.core.ext.typeinfo.JType;
import com.google.gwt.core.ext.typeinfo.NotFoundException;

/**
 * @author Jacob Hookom [jacob@hookom.net]
 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
 */
public final class AstIdentifier extends SimpleNode {
    public AstIdentifier(int id) {
        super(id);
    }

  public JClassType getReturnType(ELParserContext ctx) throws ELException, NotFoundException{
    JClassType retrunType = ctx.getLocalBeanType(this.getImage());
    if (retrunType != null){
      return retrunType;
    }
    
    String methodName = ctx.getGetMethodName(this.getImage());
    // return ctx.autoboxed(ctx.getPageContextType().getMethod(methodName, new JType[0]).getReturnType());
    return ctx.autoboxed(ELParserContext.getMethod(ctx.getPageContextType(), methodName, new JType[0]).getReturnType());
  }
  public boolean isStaticValue(ELParserContext ctx) throws ELException, NotFoundException{
    return false;
  }
  
  public String generateModelObservaleValue(ELParserContext ctx) throws ELException, NotFoundException {
    StringBuffer sb = new StringBuffer();
    // JClassType type = this.getReturnType(ctx);
    JClassType type = ctx.getPageContextType();
    ctx.addObservableBean(type.getQualifiedSourceName());
    sb.append("new " + type.getQualifiedSourceName() + "ObservableValue(null, this, \"" + getImage() + "\")");
    return sb.toString();
  }
  
  public String generateGetExpression(ELParserContext ctx) throws ELException, NotFoundException{
    StringBuffer sb = new StringBuffer();
    sb.append("__this__.");
    sb.append(ctx.getGetMethodName(this.getImage()));
    sb.append("()");
    
    return sb.toString();
  }
  
  public String generateMethodBinding(ELParserContext ctx) throws ELException, NotFoundException{
    StringBuffer sb = new StringBuffer();
    
    JClassType type = ctx.getPageContextType();
    JMethod m = null;
    // Find zero parameter method
    try{
      m = ELParserContext.getMethod(type, this.getImage(), new JType[0]);
    }catch(NotFoundException e){
      e.getMessage();
    }
    if (m != null){
      sb.append("__this__.");
      sb.append(this.getImage());
      sb.append("();");
    }
    if (m == null){
      // Find method with one parameter type of GwtEvent
      for (JMethod m2 : type.getMethods()){
        if ((m2.getName().equals(this.getImage())) && (m2.getParameters().length == 1)){
          JClassType paramType = ctx.autoboxed(m2.getParameters()[0].getType());
          if (ctx.TYPE_GwtEvent.isAssignableFrom(paramType)){
            sb.append("__this__.");
            sb.append(this.getImage());
            sb.append("(event);");
            m = m2;
            break;
          }
        }
      }
    }

    if (m == null){
      // Find method with two parameters type of GwtXP and GwtEvent
      for (JMethod m2 : type.getMethods()){
        if ((m2.getName().equals(this.getImage())) && (m2.getParameters().length == 2)){
          JClassType paramType1 = ctx.autoboxed(m2.getParameters()[0].getType());
          JClassType paramType2 = ctx.autoboxed(m2.getParameters()[1].getType());
          if (ctx.TYPE_GwtXP.isAssignableFrom(paramType1) 
              && (ctx.TYPE_GwtEvent.isAssignableFrom(paramType2))){
            sb.append("__this__.");
            sb.append(this.getImage());
            sb.append("(__this__, event);");
            m = m2;
            break;
          }
        }
      }
    }

    if (m == null){
      throw new NotFoundException("Could not generate MethodBinding for: " + this.getImage());
    }

    return sb.toString();
  }
}
